
/*
===============================================================================
                    EntitySpaces Studio by EntitySpaces, LLC
             Persistence Layer and Business Objects for Microsoft .NET
             EntitySpaces(TM) is a legal trademark of EntitySpaces, LLC
                          http://www.entityspaces.net
===============================================================================
EntitySpaces Version : 2012.1.0930.0
EntitySpaces Driver  : SQL
Date Generated       : 2016/3/21 1:39:13
===============================================================================
*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Linq;
using System.Data;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Runtime.Serialization;

using EntitySpaces.Core;
using EntitySpaces.Interfaces;
using EntitySpaces.DynamicQuery;



namespace yncTech.BusinessObjects
{
	/// <summary>
	/// Encapsulates the 'T_S_RoleFunctions' table
	/// </summary>

    [DebuggerDisplay("Data = {Debug}")]
	[Serializable]
	[DataContract]
	[KnownType(typeof(TSRoleFunctions))]	
	[XmlType("TSRoleFunctions")]
	public partial class TSRoleFunctions : esTSRoleFunctions
	{	
		[DebuggerBrowsable(DebuggerBrowsableState.RootHidden | DebuggerBrowsableState.Never)]
		protected override esEntityDebuggerView[] Debug
		{
			get { return base.Debug; }
		}

		override public esEntity CreateInstance()
		{
			return new TSRoleFunctions();
		}
		
		#region Static Quick Access Methods
		static public void Delete(System.Decimal roleID, System.Decimal functionID)
		{
			var obj = new TSRoleFunctions();
			obj.RoleID = roleID;
			obj.FunctionID = functionID;
			obj.AcceptChanges();
			obj.MarkAsDeleted();
			obj.Save();
		}

	    static public void Delete(System.Decimal roleID, System.Decimal functionID, esSqlAccessType sqlAccessType)
		{
			var obj = new TSRoleFunctions();
			obj.RoleID = roleID;
			obj.FunctionID = functionID;
			obj.AcceptChanges();
			obj.MarkAsDeleted();
			obj.Save(sqlAccessType);
		}
		#endregion

		
					
		
	
	}



    [DebuggerDisplay("Count = {Count}")]
	[Serializable]
	[CollectionDataContract]
	[XmlType("TSRoleFunctionsCollection")]
	public partial class TSRoleFunctionsCollection : esTSRoleFunctionsCollection, IEnumerable<TSRoleFunctions>
	{
		public TSRoleFunctions FindByPrimaryKey(System.Decimal roleID, System.Decimal functionID)
		{
			return this.SingleOrDefault(e => e.RoleID == roleID && e.FunctionID == functionID);
		}

		
		
		#region WCF Service Class
		
		[DataContract]
		[KnownType(typeof(TSRoleFunctions))]
		public class TSRoleFunctionsCollectionWCFPacket : esCollectionWCFPacket<TSRoleFunctionsCollection>
		{
			public static implicit operator TSRoleFunctionsCollection(TSRoleFunctionsCollectionWCFPacket packet)
			{
				return packet.Collection;
			}

			public static implicit operator TSRoleFunctionsCollectionWCFPacket(TSRoleFunctionsCollection collection)
			{
				return new TSRoleFunctionsCollectionWCFPacket() { Collection = collection };
			}
		}
		
		#endregion
		
				
	}



    [DebuggerDisplay("Query = {Parse()}")]
	[Serializable]	
	public partial class TSRoleFunctionsQuery : esTSRoleFunctionsQuery
	{
		public TSRoleFunctionsQuery():this("TSRoleFunctionsQuery")
		{
		}
		public TSRoleFunctionsQuery(string joinAlias)
		{
			this.es.JoinAlias = joinAlias;
		}	

		override protected string GetQueryName()
		{
			return "TSRoleFunctionsQuery";
		}
		
					
	
		#region Explicit Casts
		
		public static explicit operator string(TSRoleFunctionsQuery query)
		{
			return TSRoleFunctionsQuery.SerializeHelper.ToXml(query);
		}

		public static explicit operator TSRoleFunctionsQuery(string query)
		{
			return (TSRoleFunctionsQuery)TSRoleFunctionsQuery.SerializeHelper.FromXml(query, typeof(TSRoleFunctionsQuery));
		}
		
		#endregion		
	}

	[DataContract]
	[Serializable]
	abstract public partial class esTSRoleFunctions : esEntity
	{
		public esTSRoleFunctions()
		{

		}
		
		#region LoadByPrimaryKey
		public virtual bool LoadByPrimaryKey(System.Decimal roleID, System.Decimal functionID)
		{
			if(this.es.Connection.SqlAccessType == esSqlAccessType.DynamicSQL)
				return LoadByPrimaryKeyDynamic(roleID, functionID);
			else
				return LoadByPrimaryKeyStoredProcedure(roleID, functionID);
		}

		public virtual bool LoadByPrimaryKey(esSqlAccessType sqlAccessType, System.Decimal roleID, System.Decimal functionID)
		{
			if (sqlAccessType == esSqlAccessType.DynamicSQL)
				return LoadByPrimaryKeyDynamic(roleID, functionID);
			else
				return LoadByPrimaryKeyStoredProcedure(roleID, functionID);
		}

		private bool LoadByPrimaryKeyDynamic(System.Decimal roleID, System.Decimal functionID)
		{
			TSRoleFunctionsQuery query = new TSRoleFunctionsQuery();
			query.Where(query.RoleID == roleID, query.FunctionID == functionID);
			return this.Load(query);
		}

		private bool LoadByPrimaryKeyStoredProcedure(System.Decimal roleID, System.Decimal functionID)
		{
			esParameters parms = new esParameters();
			parms.Add("RoleID", roleID);			parms.Add("FunctionID", functionID);
			return this.Load(esQueryType.StoredProcedure, this.es.spLoadByPrimaryKey, parms);
		}
		#endregion
		
		#region Properties
		
		
		
		/// <summary>
		/// Maps to T_S_RoleFunctions.RoleID
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.Decimal? RoleID
		{
			get
			{
				return base.GetSystemDecimal(TSRoleFunctionsMetadata.ColumnNames.RoleID);
			}
			
			set
			{
				if(base.SetSystemDecimal(TSRoleFunctionsMetadata.ColumnNames.RoleID, value))
				{
					this._UpToTSRoleByRoleID = null;
					this.OnPropertyChanged("UpToTSRoleByRoleID");
					OnPropertyChanged(TSRoleFunctionsMetadata.PropertyNames.RoleID);
				}
			}
		}		
		
		/// <summary>
		/// Maps to T_S_RoleFunctions.FunctionID
		/// </summary>
		[DataMember(EmitDefaultValue=false)]
		virtual public System.Decimal? FunctionID
		{
			get
			{
				return base.GetSystemDecimal(TSRoleFunctionsMetadata.ColumnNames.FunctionID);
			}
			
			set
			{
				if(base.SetSystemDecimal(TSRoleFunctionsMetadata.ColumnNames.FunctionID, value))
				{
					this._UpToTSFunctionsByFunctionID = null;
					this.OnPropertyChanged("UpToTSFunctionsByFunctionID");
					OnPropertyChanged(TSRoleFunctionsMetadata.PropertyNames.FunctionID);
				}
			}
		}		
		
		[CLSCompliant(false)]
		internal protected TSFunctions _UpToTSFunctionsByFunctionID;
		[CLSCompliant(false)]
		internal protected TSRole _UpToTSRoleByRoleID;
		#endregion	

		#region .str() Properties
		
		public override void SetProperties(IDictionary values)
		{
			foreach (string propertyName in values.Keys)
			{
				this.SetProperty(propertyName, values[propertyName]);
			}
		}
		
		public override void SetProperty(string name, object value)
		{
			esColumnMetadata col = this.Meta.Columns.FindByPropertyName(name);
			if (col != null)
			{
				if(value == null || value is System.String)
				{				
					// Use the strongly typed property
					switch (name)
					{							
						case "RoleID": this.str().RoleID = (string)value; break;							
						case "FunctionID": this.str().FunctionID = (string)value; break;
					}
				}
				else
				{
					switch (name)
					{	
						case "RoleID":
						
							if (value == null || value is System.Decimal)
								this.RoleID = (System.Decimal?)value;
								OnPropertyChanged(TSRoleFunctionsMetadata.PropertyNames.RoleID);
							break;
						
						case "FunctionID":
						
							if (value == null || value is System.Decimal)
								this.FunctionID = (System.Decimal?)value;
								OnPropertyChanged(TSRoleFunctionsMetadata.PropertyNames.FunctionID);
							break;
					

						default:
							break;
					}
				}
			}
            else if (this.ContainsColumn(name))
            {
                this.SetColumn(name, value);
            }
			else
			{
				throw new Exception("SetProperty Error: '" + name + "' not found");
			}
		}		

		public esStrings str()
		{
			if (esstrings == null)
			{
				esstrings = new esStrings(this);
			}
			return esstrings;
		}

		sealed public class esStrings
		{
			public esStrings(esTSRoleFunctions entity)
			{
				this.entity = entity;
			}
			
	
			public System.String RoleID
			{
				get
				{
					System.Decimal? data = entity.RoleID;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.RoleID = null;
					else entity.RoleID = Convert.ToDecimal(value);
				}
			}
				
			public System.String FunctionID
			{
				get
				{
					System.Decimal? data = entity.FunctionID;
					return (data == null) ? String.Empty : Convert.ToString(data);
				}

				set
				{
					if (value == null || value.Length == 0) entity.FunctionID = null;
					else entity.FunctionID = Convert.ToDecimal(value);
				}
			}
			

			private esTSRoleFunctions entity;
		}
		
		[NonSerialized]
		private esStrings esstrings;		
		
		#endregion
		
		#region Housekeeping methods

		override protected IMetadata Meta
		{
			get
			{
				return TSRoleFunctionsMetadata.Meta();
			}
		}

		#endregion		
		
		#region Query Logic

		public TSRoleFunctionsQuery Query
		{
			get
			{
				if (this.query == null)
				{
					this.query = new TSRoleFunctionsQuery();
					InitQuery(this.query);
				}

				return this.query;
			}
		}

		public bool Load(TSRoleFunctionsQuery query)
		{
			this.query = query;
			InitQuery(this.query);
			return this.Query.Load();
		}
		
		protected void InitQuery(TSRoleFunctionsQuery query)
		{
			query.OnLoadDelegate = this.OnQueryLoaded;
			
			if (!query.es2.HasConnection)
			{
				query.es2.Connection = ((IEntity)this).Connection;
			}			
		}

		#endregion
		
        [IgnoreDataMember]
		private TSRoleFunctionsQuery query;		
	}



	[Serializable]
	abstract public partial class esTSRoleFunctionsCollection : esEntityCollection<TSRoleFunctions>
	{
		#region Housekeeping methods
		override protected IMetadata Meta
		{
			get
			{
				return TSRoleFunctionsMetadata.Meta();
			}
		}

		protected override string GetCollectionName()
		{
			return "TSRoleFunctionsCollection";
		}

		#endregion		
		
		#region Query Logic

	#if (!WindowsCE)
		[BrowsableAttribute(false)]
	#endif
		public TSRoleFunctionsQuery Query
		{
			get
			{
				if (this.query == null)
				{
					this.query = new TSRoleFunctionsQuery();
					InitQuery(this.query);
				}

				return this.query;
			}
		}

		public bool Load(TSRoleFunctionsQuery query)
		{
			this.query = query;
			InitQuery(this.query);
			return Query.Load();
		}

		override protected esDynamicQuery GetDynamicQuery()
		{
			if (this.query == null)
			{
				this.query = new TSRoleFunctionsQuery();
				this.InitQuery(query);
			}
			return this.query;
		}

		protected void InitQuery(TSRoleFunctionsQuery query)
		{
			query.OnLoadDelegate = this.OnQueryLoaded;
			
			if (!query.es2.HasConnection)
			{
				query.es2.Connection = ((IEntityCollection)this).Connection;
			}			
		}

		protected override void HookupQuery(esDynamicQuery query)
		{
			this.InitQuery((TSRoleFunctionsQuery)query);
		}

		#endregion
		
		private TSRoleFunctionsQuery query;
	}



	[Serializable]
	abstract public partial class esTSRoleFunctionsQuery : esDynamicQuery
	{
		override protected IMetadata Meta
		{
			get
			{
				return TSRoleFunctionsMetadata.Meta();
			}
		}	
		
		#region QueryItemFromName
		
        protected override esQueryItem QueryItemFromName(string name)
        {
            switch (name)
            {
				case "RoleID": return this.RoleID;
				case "FunctionID": return this.FunctionID;

                default: return null;
            }
        }		
		
		#endregion
		
		#region esQueryItems

		public esQueryItem RoleID
		{
			get { return new esQueryItem(this, TSRoleFunctionsMetadata.ColumnNames.RoleID, esSystemType.Decimal); }
		} 
		
		public esQueryItem FunctionID
		{
			get { return new esQueryItem(this, TSRoleFunctionsMetadata.ColumnNames.FunctionID, esSystemType.Decimal); }
		} 
		
		#endregion
		
	}


	
	public partial class TSRoleFunctions : esTSRoleFunctions
	{

				
		#region UpToTSFunctionsByFunctionID - Many To One
		/// <summary>
		/// Many to One
		/// Foreign Key Name - FK2_T_S_RoleFunctions_FunctionID
		/// </summary>

		[XmlIgnore]
					
		public TSFunctions UpToTSFunctionsByFunctionID
		{
			get
			{
				if (this.es.IsLazyLoadDisabled) return null;
				
				if(this._UpToTSFunctionsByFunctionID == null && FunctionID != null)
				{
					this._UpToTSFunctionsByFunctionID = new TSFunctions();
					this._UpToTSFunctionsByFunctionID.es.Connection.Name = this.es.Connection.Name;
					this.SetPreSave("UpToTSFunctionsByFunctionID", this._UpToTSFunctionsByFunctionID);
					this._UpToTSFunctionsByFunctionID.Query.Where(this._UpToTSFunctionsByFunctionID.Query.FunctionID == this.FunctionID);
					this._UpToTSFunctionsByFunctionID.Query.Load();
				}	
				return this._UpToTSFunctionsByFunctionID;
			}
			
			set
			{
				this.RemovePreSave("UpToTSFunctionsByFunctionID");
				

				if(value == null)
				{
					this.FunctionID = null;
					this._UpToTSFunctionsByFunctionID = null;
				}
				else
				{
					this.FunctionID = value.FunctionID;
					this._UpToTSFunctionsByFunctionID = value;
					this.SetPreSave("UpToTSFunctionsByFunctionID", this._UpToTSFunctionsByFunctionID);
				}
				
			}
		}
		#endregion
		

				
		#region UpToTSRoleByRoleID - Many To One
		/// <summary>
		/// Many to One
		/// Foreign Key Name - FK1_T_S_RoleFunctions_RoleID
		/// </summary>

		[XmlIgnore]
					
		public TSRole UpToTSRoleByRoleID
		{
			get
			{
				if (this.es.IsLazyLoadDisabled) return null;
				
				if(this._UpToTSRoleByRoleID == null && RoleID != null)
				{
					this._UpToTSRoleByRoleID = new TSRole();
					this._UpToTSRoleByRoleID.es.Connection.Name = this.es.Connection.Name;
					this.SetPreSave("UpToTSRoleByRoleID", this._UpToTSRoleByRoleID);
					this._UpToTSRoleByRoleID.Query.Where(this._UpToTSRoleByRoleID.Query.RoleID == this.RoleID);
					this._UpToTSRoleByRoleID.Query.Load();
				}	
				return this._UpToTSRoleByRoleID;
			}
			
			set
			{
				this.RemovePreSave("UpToTSRoleByRoleID");
				

				if(value == null)
				{
					this.RoleID = null;
					this._UpToTSRoleByRoleID = null;
				}
				else
				{
					this.RoleID = value.RoleID;
					this._UpToTSRoleByRoleID = value;
					this.SetPreSave("UpToTSRoleByRoleID", this._UpToTSRoleByRoleID);
				}
				
			}
		}
		#endregion
		

		
		/// <summary>
		/// Used internally for retrieving AutoIncrementing keys
		/// during hierarchical PreSave.
		/// </summary>
		protected override void ApplyPreSaveKeys()
		{
			if(!this.es.IsDeleted && this._UpToTSFunctionsByFunctionID != null)
			{
				this.FunctionID = this._UpToTSFunctionsByFunctionID.FunctionID;
			}
			if(!this.es.IsDeleted && this._UpToTSRoleByRoleID != null)
			{
				this.RoleID = this._UpToTSRoleByRoleID.RoleID;
			}
		}
		
	}
	



	[Serializable]
	public partial class TSRoleFunctionsMetadata : esMetadata, IMetadata
	{
		#region Protected Constructor
		protected TSRoleFunctionsMetadata()
		{
			m_columns = new esColumnMetadataCollection();
			esColumnMetadata c;

			c = new esColumnMetadata(TSRoleFunctionsMetadata.ColumnNames.RoleID, 0, typeof(System.Decimal), esSystemType.Decimal);
			c.PropertyName = TSRoleFunctionsMetadata.PropertyNames.RoleID;
			c.IsInPrimaryKey = true;
			c.NumericPrecision = 18;
			m_columns.Add(c);
				
			c = new esColumnMetadata(TSRoleFunctionsMetadata.ColumnNames.FunctionID, 1, typeof(System.Decimal), esSystemType.Decimal);
			c.PropertyName = TSRoleFunctionsMetadata.PropertyNames.FunctionID;
			c.IsInPrimaryKey = true;
			c.NumericPrecision = 18;
			m_columns.Add(c);
				
		}
		#endregion	
	
		static public TSRoleFunctionsMetadata Meta()
		{
			return meta;
		}	
		
		public Guid DataID
		{
			get { return base.m_dataID; }
		}	
		
		public bool MultiProviderMode
		{
			get { return false; }
		}		

		public esColumnMetadataCollection Columns
		{
			get	{ return base.m_columns; }
		}
		
		#region ColumnNames
		public class ColumnNames
		{ 
			 public const string RoleID = "RoleID";
			 public const string FunctionID = "FunctionID";
		}
		#endregion	
		
		#region PropertyNames
		public class PropertyNames
		{ 
			 public const string RoleID = "RoleID";
			 public const string FunctionID = "FunctionID";
		}
		#endregion	

		public esProviderSpecificMetadata GetProviderMetadata(string mapName)
		{
			MapToMeta mapMethod = mapDelegates[mapName];

			if (mapMethod != null)
				return mapMethod(mapName);
			else
				return null;
		}
		
		#region MAP esDefault
		
		static private int RegisterDelegateesDefault()
		{
			// This is only executed once per the life of the application
			lock (typeof(TSRoleFunctionsMetadata))
			{
				if(TSRoleFunctionsMetadata.mapDelegates == null)
				{
					TSRoleFunctionsMetadata.mapDelegates = new Dictionary<string,MapToMeta>();
				}
				
				if (TSRoleFunctionsMetadata.meta == null)
				{
					TSRoleFunctionsMetadata.meta = new TSRoleFunctionsMetadata();
				}
				
				MapToMeta mapMethod = new MapToMeta(meta.esDefault);
				mapDelegates.Add("esDefault", mapMethod);
				mapMethod("esDefault");
			}
			return 0;
		}			

		private esProviderSpecificMetadata esDefault(string mapName)
		{
			if(!m_providerMetadataMaps.ContainsKey(mapName))
			{
				esProviderSpecificMetadata meta = new esProviderSpecificMetadata();			


				meta.AddTypeMap("RoleID", new esTypeMap("numeric", "System.Decimal"));
				meta.AddTypeMap("FunctionID", new esTypeMap("numeric", "System.Decimal"));			
				
				
				
				meta.Source = "T_S_RoleFunctions";
				meta.Destination = "T_S_RoleFunctions";
				
				meta.spInsert = "proc_T_S_RoleFunctionsInsert";				
				meta.spUpdate = "proc_T_S_RoleFunctionsUpdate";		
				meta.spDelete = "proc_T_S_RoleFunctionsDelete";
				meta.spLoadAll = "proc_T_S_RoleFunctionsLoadAll";
				meta.spLoadByPrimaryKey = "proc_T_S_RoleFunctionsLoadByPrimaryKey";
				
				this.m_providerMetadataMaps["esDefault"] = meta;
			}
			
			return this.m_providerMetadataMaps["esDefault"];
		}

		#endregion

		static private TSRoleFunctionsMetadata meta;
		static protected Dictionary<string, MapToMeta> mapDelegates;
		static private int _esDefault = RegisterDelegateesDefault();
	}
}
